import { _decorator, Component, Node, Tween, tween, UITransform, v2, v3, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MenuButton')
export class MenuButton extends Component {

    transform: UITransform;
    contentSize: Vec2 = v2();

    protected onLoad(): void {
        this.transform = this.node.getComponent(UITransform);
        this.contentSize.set(this.transform.contentSize.width, this.transform.contentSize.height);
    }

    Refresh(name: string) {
        Tween.stopAllByTarget(this.node.scale);

        if (this.node.name == name) {
            tween(this.node.scale)
                .to(0.2, Vec3.ONE.clone().multiplyScalar(1.2), {
                    onUpdate: (target: Vec3, ratio: number) => {
                        this.node.scale = target;
                        this.transform.setContentSize(this.contentSize.x * this.node.scale.x, this.contentSize.y * this.node.scale.y);
                    }, easing: `sineOut`
                }).start();
        } else {
            tween(this.node.scale)
                .to(0.2, Vec3.ONE, {
                    onUpdate: (target: Vec3, ratio: number) => {
                        this.node.scale = target;
                        this.transform.setContentSize(this.contentSize.x * this.node.scale.x, this.contentSize.y * this.node.scale.y);
                    }, easing: `sineOut`
                }).start();
        }
    }
}